package com.moioo.exam;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

/**
 * A data access object (DAO) providing persistence and search support for
 * TRoleUser entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.moioo.exam.TRoleUser
 * @author MyEclipse Persistence Tools
 */

public class TRoleUserDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(TRoleUserDAO.class);

	// property constants

	public void save(TRoleUser transientInstance) {
		log.debug("saving TRoleUser instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(TRoleUser persistentInstance) {
		log.debug("deleting TRoleUser instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public TRoleUser findById(com.moioo.exam.TRoleUserId id) {
		log.debug("getting TRoleUser instance with id: " + id);
		try {
			TRoleUser instance = (TRoleUser) getSession().get(
					"com.moioo.exam.TRoleUser", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	public List<TRoleUser> findByUser(int userid) {
		log.debug("select findByUser instance with id: " + userid);
		try {
			String queryString = "select model from TRoleUser as model,TRole as r where model.id.userid = ? and model.id.roleid = r.id and r.state = 0";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, userid);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	public List<TRoleUser> findByRole(int roleid) {
		log.debug("select findByRole instance with id: " + roleid);
		List<TRoleUser> rtv = new ArrayList<TRoleUser>();
		try {
			String queryString = "select model,r.name as rname,u.name as uname , u.passport ,u.organ  from TRoleUser as model , TRole as r , TUser as u where model.id.userid = u.id and model.id.roleid = r.id and model.id.roleid = ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, roleid);
			Iterator i = queryObject.list().iterator();
			List<TOrgan> organs = TOrgan.getDAO().getAll();
			while(i.hasNext()){
				Object[] o = (Object[])i.next();
				TRoleUser obj = (TRoleUser)o[0];
				obj.setRname((String)o[1]);
				obj.setUname((String)o[2]);
				obj.setPassport((String)o[3]);
				obj.setOrgan((Integer)o[4]);
				for(TOrgan r : organs){
					if(r.getId().equals(o[4])){
						obj.setOrganName(r.getName());
						break;
					}
				}
				rtv.add(obj);
			}
			return rtv;
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	public List findByExample(TRoleUser instance) {
		log.debug("finding TRoleUser instance by example");
		try {
			List results = getSession().createCriteria(
					"com.moioo.exam.TRoleUser").add(Example.create(instance))
					.list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding TRoleUser instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from TRoleUser as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findAll() {
		log.debug("finding all TRoleUser instances");
		try {
			String queryString = "from TRoleUser";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public TRoleUser merge(TRoleUser detachedInstance) {
		log.debug("merging TRoleUser instance");
		try {
			TRoleUser result = (TRoleUser) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(TRoleUser instance) {
		log.debug("attaching dirty TRoleUser instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(TRoleUser instance) {
		log.debug("attaching clean TRoleUser instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
	public static void main(String[] argv)throws Exception{
		new TRoleUserDAO().findByRole(0);
	}
}